package com.iot.manage.dao;

import com.alibaba.fastjson.JSONObject;
import com.iot.manage.pojo.App;
import com.iot.manage.pojo.Area;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface AppMapper {

    @Select("<script>" +
            "SELECT  \n" +
            " temp.APP_ID appId,\n" +
            " temp.APP_CODE appCode,\n" +
            " temp.APP_NAME appName,\n" +
            " temp.APP_ARG appArg\n" +
            "FROM (   \n" +
            "\tSELECT  \n" +
            "\t\tROWNUM rowno,  \n" +
            "\t\tt.*  \n" +
            "\tFROM T_APP t\n" +
            "\tWHERE t.APP_NAME LIKE CONCAT(CONCAT('%', #{query}),'%') OR t.APP_CODE LIKE CONCAT(CONCAT('%', #{query}),'%') \n" +
            "\tAND ROWNUM &lt;= #{pagesize}  ORDER BY t.APP_ID\n" +
            ") temp WHERE temp.rowno &gt; #{start}" +
            "</script>")
    List<App> findAppsInfo(@Param("query") String query,
                           @Param("start") int start,
                           @Param("pagesize") int pagesize);

    @Select("<script>SELECT COUNT(a.APP_ID) FROM T_APP a WHERE a.APP_NAME LIKE CONCAT(CONCAT('%', #{query}),'%') OR a.APP_CODE LIKE CONCAT(CONCAT('%', #{query}),'%')</script>")
    int countAppsInfo(String query);

    @Delete("<script>DELETE FROM T_APP WHERE APP_ID = #{appId}</script>")
    void deleteAreaById(String appId);

    @Insert("<script>INSERT INTO T_APP (APP_ID,APP_CODE,APP_NAME" +
            "<if test=\" appArg != null\">"+
            ",APP_ARG" +
            "</if>" +
            ") VALUES (#{appId},#{appCode},#{appName}" +
            "<if test=\" appArg != null\">"+
            ",#{appArg}" +
            "</if>" +
            ")</script>")
    void addApp(JSONObject params);

    @Select("<script>" +
            "SELECT  \n" +
            " temp.APP_ID appId,\n" +
            " temp.APP_CODE appCode,\n" +
            " temp.APP_NAME appName,\n" +
            " temp.APP_ARG appArg\n" +
            "FROM T_APP temp\n" +
            "\tWHERE temp.APP_ID = #{params} OR temp.APP_CODE = #{params}\n" +
            "</script>")
    App findAppByIdOrCode(String params);

    @Update("<script>" +
            "UPDATE T_APP SET \n" +
            "\tAPP_ID = #{appId},\n" +
            "\tAPP_CODE = #{appCode},\n" +
            "\tAPP_NAME = #{appName}\n" +
            "<if test=\" appArg != null\">"+
            "\t,APP_ARG = #{appArg}\n" +
            "</if>" +
            "WHERE APP_ID = #{appId}" +
            "</script>")
    void updateAppInfo(JSONObject params);

    @Select("<script>SELECT a.AREA_ID areaId,a.AREA_NAME areaName FROM T_AREA a</script>")
    List<Area> findAllAllotArea();

    @Select("<script>SELECT a.AREA_ID areaId FROM T_AREA a WHERE a.AREA_ID IN (SELECT AREA_ID FROM T_APP_AREA WHERE APP_ID = #{appId})</script>")
    List<String> findAllotAreaById(String appId);

    @Delete("<script>DELETE FROM T_APP_AREA WHERE APP_ID = #{appId}</script>")
    void deleteAreasOfAllotById(String appId);

    @Insert("<script>" +
            "INSERT INTO T_APP_AREA (APP_ID,AREA_ID)\n" +
            "<foreach collection =\"list\" item=\"appArea\" separator =\"union all\">\n" +
            "\t\t (select\n#{appArea.appId},#{appArea.areaId} from dual )\n" +
            "</foreach >" +
            "</script>")
    void addAreasOfAllotById(List<Map<String, String>> list);

    @Delete("<script>DELETE FROM T_USER_APP WHERE APP_ID = #{appId}</script>")
    void deleteRelevancyUserById(String appId);

    @Delete("<script>DELETE FROM T_APP_AREA WHERE APP_ID = #{appId}</script>")
    void deleteRelevancyAreaById(String appId);
}
